package com.gg.haoqi.ui;

import android.content.Intent;
import android.support.v7.app.ActionBarActivity;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.TextView;

import com.alibaba.fastjson.JSONObject;
import com.gg.haoqi.BaseApp;
import com.gg.haoqi.R;
import com.gg.haoqi.adapter.TenAdapter;
import com.gg.haoqi.bean.YjsEntity;
import com.gg.haoqi.bean.YjsList;
import com.loopj.android.http.AsyncHttpClient;
import com.loopj.android.http.TextHttpResponseHandler;

import org.androidannotations.annotations.AfterViews;
import org.androidannotations.annotations.EActivity;
import org.androidannotations.annotations.ViewById;

import java.util.ArrayList;

import cz.msebera.android.httpclient.Header;

@EActivity(R.layout.activity_tenimg)
public class TenImgActivity extends ActionBarActivity {

    @ViewById(R.id.pic_listview)
    ListView picListview;
    @ViewById(R.id.ten_title)
    TextView tenTitle;

    String url;
    String titleName;
    ArrayList<YjsList> yjsList;
    TenAdapter tenAdapter;

    @AfterViews
    public void init(){
        getActionBar().hide();

        url = getIntent().getStringExtra("url");
        titleName = getIntent().getStringExtra("titleName");
        tenTitle.setText(titleName);
        yjsList = new ArrayList<YjsList>();
        tenAdapter = new TenAdapter(yjsList,this);
        picListview.setAdapter(tenAdapter);
        initData(url);

        picListview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Intent intent = new Intent(TenImgActivity.this, ContentActivity_.class);
                intent.putExtra("url", yjsList.get(position).getPost().getAppview());
                startActivity(intent);
            }
        });
    }

    public void initData(String url) {
        AsyncHttpClient client = new AsyncHttpClient();
        client.get(url, new TextHttpResponseHandler() {
            @Override
            public void onFailure(int statusCode, Header[] headers, String responseString, Throwable throwable) {
                BaseApp.getInstance().showToast("数据加载失败！");
            }

            @Override
            public void onSuccess(int statusCode, Header[] headers, String responseString) {
                YjsEntity entity = JSONObject.parseObject(responseString, YjsEntity.class);
                if (entity != null) {
                    yjsList.addAll(entity.getResponse().getFeeds().getList());
                    tenAdapter.notifyDataSetChanged();
                }
            }
        });
    }

}
